package android.gpstrac.app;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.gpstrac.controls.GPSTracUpdateService;
import android.gpstrac.data.User.UserContent;
import android.gpstrac.R;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;

/**
 * The main running point for the friend tracker application.
 *
 * @author Nicolas Papin
 */
public class GPSTracApp extends Activity
{
	//
	// CLASS AND INSTANCE DATA
	//
	/**
	 * Cursor storing the currently discovered friends list.
	 */
	private Cursor mCur;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{		
		super.onCreate(savedInstanceState);
		
		startService(new Intent(GPSTracUpdateService.class.getName()));
		
		// Set our layout.
		setContentView(R.layout.main);
		
		// Get the cursor for our friends list.
		mCur = this.getContentResolver().query(UserContent.CONTENT_URI, null, null, null, null);
		
		// Set up our adapter.
		ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, mCur, new String[] { UserContent.NAME }, new int[] { R.id.friend_list_item_text });
		
		// Attach our adapter to the list.
		final ListView list = (ListView)findViewById(R.id.friend_list);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
		    {
		    	// View the page for an individual friend in the list.
		    	String name = ((TextView)view).getText().toString();
		    	viewFriendPage(name);
		    }
		});
		
		
		// Set the view map button.
		final Button viewMap = (Button)findViewById(R.id.view_map_button);
		viewMap.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
                // Call the activity's view map method.
            	viewLocationMap();
            }
        });
		
		// Set the add friend button.
		final Button addButton = (Button)findViewById(R.id.friend_add_button);
		//addButton.setEnabled(false);
		addButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
                //Call the activity's addFriend method.
            	addFriend();
            }
        });
		
		// Add enter key listener for adding friend as well.
		final EditText edittext = (EditText) findViewById(R.id.friend_name_text_box);
		edittext.setOnKeyListener(new View.OnKeyListener() {
		    public boolean onKey(View v, int keyCode, KeyEvent event) {
		        // If the event is a key-down event on the "enter" button
		        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) 
		        {
		        	// Call the activity's addFriend method.
		        	addFriend();
		        	return true;
		        }
		        return false;
		    }
		});
	}
	
	/**
	 * Switches to viewing the map for the current positions of the friends.
	 */
	private void viewLocationMap()
	{
		startActivity(new Intent(GPSTracMap.class.getName()));
	}
	
	/**
	 * Sends a notification to the running service to add a friend for the current user.
	 */
	private void addFriend()
	{
		final EditText edittext = (EditText) findViewById(R.id.friend_name_text_box);
		String name = edittext.getText().toString();
		
		Intent addFriend = new Intent(GPSTracUpdateService.class.getName() + ".addFriend");
		addFriend.putExtra("username", name);
		sendBroadcast(addFriend);
	}
	
	/**
	 * Brings up the friend specific page for the clicked friend.
	 */
	private void viewFriendPage(String friendName)
	{
		Intent contIntent = new Intent(GPSTracFriendHistApp.class.getName());
		contIntent.putExtra("wUserHist", friendName);
		
		Log.i("GPSTracApp", contIntent.getAction());
		
		startActivity(contIntent);
	}
}
